package atguigu.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;


public class ParallelStreamDemo5 {
	
	
	public static void main(String[] args) {
		
		String[] type = new String[] {"钢铁","冶金","金融","交通","服务"};
        List<Foo> fl = new ArrayList<>();
        for (int j = 1; j <= 10000000; j++) {
        	Foo foo = new Foo();
        	foo.setNum(j);
        	foo.setType(type[(j%5)]);
            fl.add(foo);
        }
        AtomicInteger total = new AtomicInteger();
        Map<String, AtomicInteger> map = new ConcurrentHashMap<String, AtomicInteger>();
        fl.parallelStream().forEach(l->{
        	total.incrementAndGet();
        	String key = l.getType();
        	if(map.get(key) == null) {
        		AtomicInteger count = new AtomicInteger(1);
        		map.put(key, count);
        	}else {
        		AtomicInteger count = map.get(key);
        		count.incrementAndGet();
    			map.put(key,count);
        	}
        });
        System.out.println("total:"+total.get());
        Set<Entry<String, AtomicInteger>> entrySet = map.entrySet();
        for (Entry<String, AtomicInteger> entry : entrySet) {
        	System.out.println(entry.getKey()+"\t"+entry.getValue().get());
		}
    }
}
